package messages;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import net.GameNetMessages;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import gui.BoardGUI;
import logic.Team;

public class RotatePiece implements GameMessage {
	private int x;
	private int y;
	private String direction;
	private Team player;

	public final static String LEFT = "left";
	public final static String RIGHT = "right";
	
	public RotatePiece(int x, int y, String direction, Team player){
		this.x = x;
		this.y = y;
		this.direction = direction;
		this.player = player;
	}
	
	public int getX(){
		return x;
	}
	
	public int getY(){
		return y;
	}
	
	public String getDirection(){
		return direction;
	}
	
	public Team getTeam(){
		return player;
	}

	@Override
	public String getMessage() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setMessage(String message) {
		// TODO Auto-generated method stub

	}
	
	public Document getXMLMessage(){
		Document msg;
		try {
			msg = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
			Element rotMsg = msg.createElement(GameNetMessages.khetMsg);
			msg.appendChild(rotMsg);
			rotMsg.setAttribute("type", GameNetMessages.rotate);
			rotMsg.setAttribute("x", x+"");
			rotMsg.setAttribute("y", y+"");
			rotMsg.setAttribute("direction", direction+"");
			return msg;
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
	
	public boolean rotatePiece(BoardGUI board){
		if(direction.equals(LEFT)){
			return board.rotatePieceLeft(x, y, player);
		}else if(direction.equals(RIGHT)){
			return board.rotatePieceRight(x, y, player);
		}
		return false;
	}

}
